﻿using Common.Net;
using KCPServer.Core.Net.Info;
using KCPServer.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KCPServer.Core.Net
{
    public partial class NetManager:Singleton<NetManager>
    {
        private Dictionary<RequestType,Action<MsgInfoPack>> msgDic = new Dictionary<RequestType, Action<MsgInfoPack>>();
        public void Init() {
            Console.WriteLine("网络服务开始初始化");

            ////注册消息回调
            RegisterCallback();

            NServer.Instance.Start();
            NServer.HandlerController += DispatchMsg;


        }

        private void DispatchMsg(MsgInfo info)
        {
            if (msgDic.ContainsKey(info.requestType))
            {
                var msg = new MsgInfoPack(NServer.Instance.clientPeerDic[info.sendId], info);
                msgDic[info.requestType].Invoke(msg);
            }
        }

        public void Update()
        {
            NServer.Instance.DispatchMsgInfo();
        }
    }
}
